package com.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.common.uilt.BaseAction;
import com.common.uilt.ConfigUtil;
import com.google.gson.Gson;
import com.model.AssetsBean;
import com.model.Messages;
import com.model.ReceiptBean;
import com.model.ReceiptDetailsBean;
import com.service.imp.AssetsService;
import com.service.imp.MeasureService;
import com.service.imp.ReceiptService;
import com.service.imp.UserService;
@Controller
@ParentPackage("PDpackage")
@Namespace("/back")
@Action(value = "assetsMS", results = {
		@Result(name="browseentry",		location="/back/assetsMS/browse_entry.jsp"),
		@Result(name="neworeditassets",	location="/back/assetsMS/neworedit_assets.jsp"),
		@Result(name="out_depot",		location="/back/assetsMS/receipt_out_depot.jsp"),
		@Result(name="refunding",		location="/back/assetsMS/receipt_refunding.jsp"),
		@Result(name="allocation",		location="/back/assetsMS/receipt_allocation.jsp"),
		@Result(name="loan",			location="/back/assetsMS/receipt_loan.jsp"),
		@Result(name="return_assets",	location="/back/assetsMS/receipt_return_assets.jsp"),
		@Result(name="carry",			location="/back/assetsMS/receipt_carry.jsp"),
		@Result(name="scrapped",		location="/back/assetsMS/receipt_scrapped.jsp"),
		@Result(name="search_ifrom",	location="/back/assetsMS/Search_ifrom.jsp"),
		@Result(name="depreciation",	location="/back/assetsMS/depreciationMS.jsp"),
		@Result(name="ref_out_depot",	location="/back/assetsMS/receipt_ref_out_depot.jsp"),
		@Result(name="reportform",		location="/back/assetsMS/reportform.jsp"),
		@Result(name="receiptMS",		location="/back/assetsMS/receiptMS.jsp"),
		@Result(name="inventory",		location="/back/assetsMS/inventory.jsp"),
		@Result(name = "error", 		location = "/error.jsp"), })
public class AssetsMsAction extends BaseAction{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 67285778651380430L;

	@Autowired
	private MeasureService measureServic;
	@Autowired
	private AssetsService assetsService;
	@Autowired
	private UserService userService;
	@Autowired
	private ReceiptService receiptService;
	private String id;
	private String test;
	private List<AssetsBean> assetslist;
	private List<ReceiptDetailsBean> receiptdetailslist;
	private ReceiptBean receiptBean;
	private String is_auto_number;
	private String fieldName;
	private String name;
	private AssetsBean ab;
	
	/**
	 * 折旧页面
	 * @return
	 */
	public String depreciation(){
		try {
			request.setAttribute("date", new Date());
//			if(!this.receiptService.isMonthPD()){
//				request.setAttribute("tip", "本月已经盘点，将不可以进行折旧操作");
//				
//			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return "depreciation";
	}
	/**
	 * 折旧
	 * @return
	 */
	public String fold() throws Exception{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		Messages m= new Messages();
	
		try {
			if(!this.receiptService.isMonthPD()){
				m.setInfo(false);
				m.setText("本月已经盘点，将不可以进行折旧操作");
			}else{
				m=this.assetsService.fold();
			}
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText("发生错误");
		}
		out.print(gson.toJson(m));
		return NONE;
	}
	
	/**
	 * 出库
	 * @return
	 */
	public String out_depot(){
		try {
			request.setAttribute("date", new Date());
			request.setAttribute("receipt_no", this.receiptService.getReceiptNO("CK"));
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
			
		}
		return "out_depot";
	}
	/**
	 * 编辑本月，在没有折旧之前的出库单
	 * @return
	 */
	public String ref_out_depot(){
		
		return "ref_out_depot";
	}
	/**
	 * 保存出库单
	 * @return
	 */
	public String sevaReceipt(){
		try {
			if(!this.receiptService.isMonthPD()){
				request.setAttribute("tip", "发生错误");
				return "error";
			}else{
				this.receiptService.sevaReceipt(this.receiptBean,this.receiptdetailslist,this.session,"CK");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return NONE;
	}
	
	/**
	 * 获取闲置资产
	 * @return
	 */
	public String search_ifrom(){
		try {
			this.assetsService.getAssetsBystatus(currentPage,sortField,orderType,maxrecord,request);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		
		return "search_ifrom";
	}
	/**
	 * 退库
	 * @return
	 */
	public String refunding(){
		try {
			request.setAttribute("date", new Date());
			request.setAttribute("receipt_no", this.receiptService.getReceiptNO("TK"));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "refunding";
	}
	
	/**
	 * 保存退库单
	 * @return
	 */
	public String sevaRefundingReceipt(){
		try {
			if(!this.receiptService.isMonthPD()){
				request.setAttribute("tip", "本月已经盘点");
				return "error";
			}else{
				this.receiptService.sevaReceipt(this.receiptBean,this.receiptdetailslist,this.session,"TK");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return NONE;
	}
	/**
	 * 调拨
	 * @return
	 */
	public String allocation(){
		try {
			request.setAttribute("date", new Date());
			request.setAttribute("receipt_no", this.receiptService.getReceiptNO("DB"));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "allocation";
	}
	/**
	 * 保存调拨单
	 * @return
	 */
	public String sevaAllocationReceipt(){
		try {
			if(!this.receiptService.isMonthPD()){
				request.setAttribute("tip", "本月已经盘点");
				return "error";
			}else{
				this.receiptService.sevaReceipt(this.receiptBean,this.receiptdetailslist,this.session,"DB");
				//this.receiptService.sevaShift("调拨");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return NONE;
	}
	/**
	 * 借出
	 * @return
	 */
	public String loan(){
		try {
			request.setAttribute("date", new Date());
			request.setAttribute("receipt_no", this.receiptService.getReceiptNO("JC"));
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return "loan";
	}
	/**
	 * 归还
	 * @return
	 */
	public String return_assets(){
		try {
			request.setAttribute("date", new Date());
			request.setAttribute("receipt_no", this.receiptService.getReceiptNO("GH"));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "return_assets";
	}
	/**
	 * 送修
	 * @return
	 */
	public String carry(){
		return "carry";
	}
	/**
	 * 报废
	 * @return
	 */
	public String scrapped(){
		return "scrapped";
	}
	/**
	 * 新增资产页面
	 * @return
	 * @throws Exception 
	 */
	public String newOreditAssets() throws Exception{
		try {
			if(id!=null)
				request.setAttribute("assetsBean", null);
			request.setAttribute("date", new Date());
			request.setAttribute("receipt_no", this.receiptService.getReceiptNO("RK"));
			request.setAttribute("assetsStoreList", this.measureServic.list("存放地点"));
			request.setAttribute("assetsSourceList", this.measureServic.list("资产来源"));
			request.setAttribute("assetsMeasureList",this.measureServic.list("计量单位"));
			request.setAttribute("assetsSuppliersList", this.measureServic.getSuppliersList());
			request.setAttribute("assetsnumber", ConfigUtil.gerSerial(this.configpath));
			request.setAttribute("is_auto_number", ConfigUtil.getFileIo("is_auto_number", this.configpath));
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		
		return "neworeditassets";
	}
	/**
	 * 保存资产
	 * @return
	 */
	public String saveAll(){
		try {
			this.assetsService.sevaAll(receiptBean,assetslist,this.configpath,session);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return NONE;
	}
	/**
	 * 修改资产
	 * @return
	 * @throws Exception
	 */
	public String editassets() throws Exception{
		PrintWriter out = response.getWriter();
		Messages m= new Messages();
		Gson gson = new Gson();
		try {
			m= this.assetsService.editassets(ab);
		} catch (Exception e) {
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 删除资产
	 * @return
	 */
	public String delassets() throws Exception{
		PrintWriter out = response.getWriter();
		Messages m= new Messages();
		Gson gson = new Gson();
		try {
			m= this.assetsService.delAssets(id);
		} catch (Exception e) {
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 获取在用资产
	 * @return
	 * @throws Exception
	 */
	public String getAssetsByfield() throws Exception{
		
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();

		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list=this.assetsService.getAssetsByfield(fieldName, name);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		out.print(gson.toJson(list));
		out.flush();
		out.close();
		return NONE;
	}
	public String receiptMS(){
		return "receiptMS";
	}
	public String getAssetsBydept() throws Exception{
		PrintWriter out = response.getWriter();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Gson gson = new Gson();
		try {
			list= this.assetsService.getAssetsBydept(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.print(gson.toJson(list));
		out.flush();
		out.close();
		return NONE;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTest() {
		return test;
	}
	public void setTest(String test) {
		this.test = test;
	}
	public List<AssetsBean> getAssetslist() {
		return assetslist;
	}
	public void setAssetslist(List<AssetsBean> assetslist) {
		this.assetslist = assetslist;
	}
	public String getIs_auto_number() {
		return is_auto_number;
	}
	public void setIs_auto_number(String is_auto_number) {
		this.is_auto_number = is_auto_number;
	}
	public List<ReceiptDetailsBean> getReceiptdetailslist() {
		return receiptdetailslist;
	}
	public void setReceiptdetailslist(List<ReceiptDetailsBean> receiptdetailslist) {
		this.receiptdetailslist = receiptdetailslist;
	}
	public ReceiptBean getReceiptBean() {
		return receiptBean;
	}
	public void setReceiptBean(ReceiptBean receiptBean) {
		this.receiptBean = receiptBean;
	}
	public String getFieldName() {
		return fieldName;
	}
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public AssetsBean getAb() {
		return ab;
	}
	public void setAb(AssetsBean ab) {
		this.ab = ab;
	}
	
	
	
}
